package com.vsd.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.demo.dialet.PaginationSupport;
import com.demo.util.SqlUtils;
import com.demo.util.StringUtil;
import com.vsd.daoEx.model.ChangeLogEx;
import com.vsd.domain.ChangeLog;
import com.vsd.repository.ChangeLogRepository;
import com.vsd.service.ChangeLogService;
import com.zy.credit.proto.MOCredit.MCreditGoods;
import com.zy.credit.proto.MOCredit.MCreditGoodsList;

@Service
@Transactional
public class ChangeLogServiceImpl implements ChangeLogService {

	@SuppressWarnings("unused")
	private static final Logger LOGGER = LoggerFactory
			.getLogger(ChangeLogServiceImpl.class);
	
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	@Autowired
	private ChangeLogRepository changeLogRepository;

	@Override
	public Page<ChangeLog> findByUserId(String userId, Pageable p) {
		return changeLogRepository.findByUserId(userId, p);
	}
 

	@Override
	public MCreditGoodsList.Builder findByUserId(String userId, Integer page,Integer limit) {
		MCreditGoodsList.Builder ret = MCreditGoodsList.newBuilder();
		String sql = "select g.*,l.state from s_change_log l	left join s_credit_goods g on l.fid=g.id	"
				+ "where l.user_id = '"+userId+"' order by l.create_time desc limit "+(page*limit)+","+limit;
		List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
		for (Map<String, Object> map : maps) {
			MCreditGoods.Builder m = MCreditGoods.newBuilder();
			m.setName(StringUtil.obj2String(map.get("name")));
			m.setImg(StringUtil.obj2String(map.get("img")));
			m.setCredit(map.get("num")==null?0:new Integer(map.get("num").toString()));
			m.setId(StringUtil.obj2String(map.get("id")));
			if(map.get("state")!=null){
				m.setState(new Integer(map.get("state").toString()));
			}
			ret.addList(m);
		}
		return ret;
	}


	@Override
	public List<ChangeLogEx> selectExList(String nickName) {
		List<ChangeLogEx> ret = new ArrayList<>();
		
		// TODO Auto-generated method stub
		StringBuffer sql  = new StringBuffer("select l.*,g.title as goodsName,g.img as goodsImg"
				+ ",u.nick_name as nickName,u.image as headImg from s_change_log l left join s_credit_goods g on l.fid = g.id left join f_user u on u.id = l.user_id where 1=1");
		if(StringUtils.isNotBlank(nickName)){
			sql.append(" and u.nick_name like '"+nickName+"%' ");
		}
		sql.append(" order by l.create_time desc");
		
		String s = PaginationSupport.initPage(sql.toString(), jdbcTemplate);
		List<Map<String, Object>> list = jdbcTemplate.queryForList(s);
		for (Map<String, Object> map : list) {
			ChangeLogEx e = new ChangeLogEx();
			SqlUtils.transMap2Bean2(map, e);
			ret.add(e);
		}
		return ret;
	}


	@Override
	public int updateByPrimaryKeySelective(ChangeLog log) {
		// TODO Auto-generated method stub
		try {
			String sql = SqlUtils.update(log, true, " where id = '"+log.getId()+"'");
			return jdbcTemplate.update(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return 0;
		}
	}
 

	
}
